using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using System.IO;

namespace ImageCSharp.Core.Models
{
    /// <summary>
    /// 图像文档模型
    /// </summary>
    public class ImageDocument
    {
        /// <summary>
        /// 图像文件路径
        /// </summary>
        public string FilePath { get; set; } = string.Empty;

        /// <summary>
        /// 图像对象
        /// </summary>
        public Image<Rgba32> Image { get; set; }

        /// <summary>
        /// 图像名称
        /// </summary>
        public string Name { get; set; } = "Untitled";

        /// <summary>
        /// 是否已修改
        /// </summary>
        public bool IsModified { get; set; }

        /// <summary>
        /// 缩放比例
        /// </summary>
        public float ZoomLevel { get; set; } = 1.0f;

        /// <summary>
        /// 选择区域
        /// </summary>
        public RectangleF SelectionArea { get; set; }

        /// <summary>
        /// 图层列表
        /// </summary>
        public List<Layer> Layers { get; set; } = new List<Layer>();

        /// <summary>
        /// 当前活动图层索引
        /// </summary>
        public int ActiveLayerIndex { get; set; }

        public ImageDocument()
        {
            Image = new Image<Rgba32>(800, 600);
            Layers.Add(new Layer("背景", Image.Width, Image.Height));
        }

        public ImageDocument(string filePath) : this()
        {
            FilePath = filePath;
            Name = Path.GetFileNameWithoutExtension(filePath);
            
            if (File.Exists(filePath))
            {
                Image = Image<Rgba32>.Load<Rgba32>(filePath);
                Layers.Clear();
                Layers.Add(new Layer("背景", Image.Width, Image.Height, Image));
            }
        }

        public ImageDocument Clone()
        {
            var clone = new ImageDocument();
            clone.FilePath = this.FilePath;
            clone.Name = this.Name;
            clone.IsModified = this.IsModified;
            clone.ZoomLevel = this.ZoomLevel;
            clone.SelectionArea = this.SelectionArea;
            clone.ActiveLayerIndex = this.ActiveLayerIndex;
            
            // 克隆图像
            if (this.Image != null)
            {
                clone.Image = this.Image.Clone();
            }
            
            // 克隆图层
            clone.Layers.Clear();
            foreach (var layer in this.Layers)
            {
                clone.Layers.Add(layer.Clone());
            }
            
            return clone;
        }
    }

    /// <summary>
    /// 图层模型
    /// </summary>
    public class Layer
    {
        public string Name { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }
        public Image<Rgba32> Image { get; set; }
        public bool IsVisible { get; set; } = true;
        public float Opacity { get; set; } = 1.0f;
        public Point Position { get; set; }

        public Layer(string name, int width, int height, Image<Rgba32>? image = null)
        {
            Name = name;
            Width = width;
            Height = height;
            Image = image ?? new Image<Rgba32>(width, height);
        }

        public Layer Clone()
        {
            var clone = new Layer(this.Name, this.Width, this.Height);
            clone.IsVisible = this.IsVisible;
            clone.Opacity = this.Opacity;
            clone.Position = this.Position;
            
            if (this.Image != null)
            {
                clone.Image = this.Image.Clone();
            }
            
            return clone;
        }
    }
}